package com.example.demo.service.impl;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.MaterialMap;
import com.example.demo.po.Material;
import com.example.demo.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MaterialServiceImpl implements MaterialService {

    @Autowired
    MaterialMap materialMap;

    @Override
    public Result SaveMaterial(Material material) {
        System.out.println(material);
        //重名
        List<Material> list = materialMap.GetMaterialsByName(material.getName());
        if( list.size()>0&&list.get(0).getId()!=material.getId()) {
            return ResultUtil.fail("名称重复了");
        }
        materialMap.SaveMaterial(material);
        return ResultUtil.success(material);


    }

    @Override
    public Result UpdateMaterial(Material material) {

       if(StringUtils.isEmpty(material.getName())){
           return ResultUtil.fail("名称不能为空");
       }

        List<Material> list = materialMap.GetMaterialsByName(material.getName());
        if( list.size()>0&&list.get(0).getId()!=material.getId()) {
            return ResultUtil.fail("名称重复了");
        }

        materialMap.UpdateMaterial(material);

        return ResultUtil.success("更新成功");
    }
}
